<?php

require_once 'AppMessages.php';
require_once 'ConnectDb.php';
 
class ConnexionService {

	public function setUserConnected( $email, $password, $connected ){
		$value = ( $connected == 1 ) ? 1 : 0;
		$requete = 'UPDATE players SET connected='.$value.' WHERE password=:password AND email=:email';
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		
		$prep = $connectDb->prepare( $requete );
		$prep->bindValue(':password', $password, PDO::PARAM_STR);
		$prep->bindValue(':email', $email, PDO::PARAM_STR);
		$prep->execute();
		
		return ( $value == 0 ) ? AppMessages::ON_DISCONNECT : AppMessages::ON_CONNECT;
	}
	
	public function isUserConnected($email){
		$requete = 'SELECT connected FROM players WHERE email=:email';
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		
		$prep = $connectDb->prepare( $requete );
		$prep->bindValue(':email', $email, PDO::PARAM_STR);
		$prep->execute();
		
		$data = $prep->fetch();
		
		return ( (int)($data["connected"]) == 1 ) ? AppMessages::CONNECTED : AppMessages::DISCONNECTED;
	}	
	
	public function refreshConnectedUsers() {
	
		// le client doit avoir au moins une activité enregistrée dans les 3 dernières minutes
		// sinon on le considere comme déconnecté
		$lastActivityTimeAllowed = time() - 180; 
		$requete = "UPDATE players SET connected = 0 WHERE lastActiveTime < ".$lastActivityTimeAllowed;
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		$prep = $connectDb->prepare( $requete );
		$prep->execute();
		return AppMessages::REFRESH_CONNECTED_USERS;
	}
	
	public function connectedUsers() {	
		$requete = 'SELECT name FROM players WHERE connected=1';
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		
		$prep = $connectDb->prepare( $requete );
		$prep->execute();
		$data = $prep->fetchAll();
		
		if ( !$data )
		{
			return array();
		}
		
		$max = count($data);
		$users = array();
		
		for ( $i = 0; $i < $max; $i++ ) {
			$users[] = $data[$i]["name"];
		}
		
		return $users;
	}

    public function inscription($name, $password, $email) {
	
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		
		$key = sha1($email);
		$url = "http://localhost/mobilegame/server/Amfphp/Services/ConfirmationService.php?key=".$key ;
		
		$requete = 'INSERT INTO players_notconfirm VALUES("", :name, :password, :email, NOW(), :confirmKey)';
		$prep = $connectDb->prepare( $requete );
		
		$prep->bindValue(':name', $name, PDO::PARAM_STR);
		$prep->bindValue(':password', $password, PDO::PARAM_STR);
		$prep->bindValue(':email', $email, PDO::PARAM_STR);
		$prep->bindValue(':confirmKey', $key, PDO::PARAM_STR);
		
		$prep->execute();
		//mail( $name, "confirmation inscription", $url );
		
		return AppMessages::INSCRIPTION_DONE;
	}
	
	public function confirmInscription($name, $password, $email) {
	
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		
		$requete = 'INSERT INTO players VALUES("", :name, :password, :email, NOW(), 1, '.time().' )';
		$prep = $connectDb->prepare( $requete );
		
		$prep->bindValue(':name', $name, PDO::PARAM_STR);
		$prep->bindValue(':password', $password, PDO::PARAM_STR);
		$prep->bindValue(':email', $email, PDO::PARAM_STR);
		
		$prep->execute();
		return AppMessages::INSCRIPTION_DONE;
	}
	
	public function isAlreadyIn( $p_email ){
		$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
		
		$requete = 'SELECT COUNT(*) as num FROM players WHERE email=:email';
		$prep = $connectDb->prepare( $requete );
		$prep->bindValue(':email', $p_email, PDO::PARAM_STR);
		$prep->execute();
		$count = $prep->fetch();
		
		return ( (int)($count["num"]) > 0 );
	}
	
	public function deleteUser( $email, $password){
		$connectDb = ConnectDb::getInstance()->getConnexion();
		
		$requete = 'DELETE FROM players WHERE email=:email AND password = :password';
		$prep = $connectDb->prepare( $requete );
		$prep->bindValue(':email', $email, PDO::PARAM_STR);
		$prep->bindValue(':password', $password, PDO::PARAM_STR);
		$prep->execute();
		
		return AppMessages::DELETE_DONE;
	}
	
	public function setOnVacation( $email, $password){
		$connectDb = ConnectDb::getInstance()->getConnexion();
		
		$status = 3;
		$requete = 'UPDATE  players SET connected='.$status.' WHERE email=:email AND password = :password';
		$prep = $connectDb->prepare( $requete );
		$prep->bindValue(':email', $email, PDO::PARAM_STR);
		$prep->bindValue(':password', $password, PDO::PARAM_STR);
		$prep->execute();
		
		return AppMessages::VACATION_DONE;
	}

}
?>
